package com.bktech.stupidnews.controlpanel;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bktech.stupidnews.R;

public class MyEditListAdapter extends BaseExpandableListAdapter {
	LayoutInflater inflate;
	ArrayList<Category> listCategory;
	Resources resource;
	ArrayList<ArrayList<CheckBoxState>> listChecked;
	int countChildRemove;

	public MyEditListAdapter(Context context, ArrayList<Category> data) {
		resource = context.getResources();
		inflate = LayoutInflater.from(context);
		listCategory = data;
		listChecked = new ArrayList<ArrayList<CheckBoxState>>();
		for (int i = 0; i < listCategory.size(); i++) {
			ArrayList<CheckBoxState> cateNumberI = new ArrayList<CheckBoxState>();
			for (int j = 0; j < listCategory.get(i).listChild.size(); j++) {
				cateNumberI.add(new CheckBoxState(false));
			}
			listChecked.add(cateNumberI);
		}
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return listCategory.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return listCategory.get(groupPosition).listChild.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return listCategory.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return listCategory.get(groupPosition).listChild.get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return groupPosition * 100 + childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflate.inflate(R.layout.panel_setting_category,
					parent, false);
		}

		ImageView im = (ImageView) convertView
				.findViewById(R.id.image_category);
		if (listCategory.get(groupPosition).expand) {
			im.setImageDrawable(resource
					.getDrawable(R.drawable.ic_action_next_item));
			((ExpandableListView) parent).expandGroup(groupPosition);
		} else {
			im.setImageDrawable(resource
					.getDrawable(R.drawable.ic_action_expand));
			((ExpandableListView) parent).collapseGroup(groupPosition);
		}

		TextView tv = (TextView) convertView.findViewById(R.id.text);
		tv.setText(listCategory.get(groupPosition).name);
		Log.d("GET", "getGroupView");
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflate.inflate(R.layout.panel_setting_rss_choose,
					parent, false);
		}

		ImageView checkBox = (ImageView) convertView
				.findViewById(R.id.check_box);
		if (listChecked.get(groupPosition).get(childPosition).state) {
			checkBox.setImageDrawable(resource
					.getDrawable(R.drawable.ic_checkbox_checked));
		} else {
			checkBox.setImageDrawable(resource
					.getDrawable(R.drawable.ic_checkbox_uncheck));
		}
		TextView text = (TextView) convertView.findViewById(R.id.rss_text);
		text.setText(listCategory.get(groupPosition).getChild(childPosition).title);

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	public Category getCategory(int pos) {
		return listCategory.get(pos);
	}

	public void checkTo(int groupPos, int childPos, Button btnRemove) {
		boolean currentState = listChecked.get(groupPos).get(childPos).state;
		listChecked.get(groupPos).get(childPos).state = !currentState;
		countChildRemove = 0;
		for (int categoryIndex = 0; categoryIndex < listChecked.size(); categoryIndex++) {
			ArrayList<CheckBoxState> childList = listChecked.get(categoryIndex);
			for (int childIndex = 0; childIndex < childList.size(); childIndex++) {
				if (childList.get(childIndex).state) {
					countChildRemove++;
				}
			}
		}
		if (countChildRemove == 0) {
			btnRemove.setText("Remove");
			btnRemove.setTextColor(resource.getColor(R.color.color_text_child));
		} else {
			btnRemove.setText("Remove(" + countChildRemove + ")");
			btnRemove.setTextColor(resource.getColor(android.R.color.white));
		}
		notifyDataSetChanged();
	}

	public void removeRSS(Button btnRemove) {
		for (int categoryIndex = 0; categoryIndex < listChecked.size(); categoryIndex++) {
			ArrayList<CheckBoxState> listChildState = listChecked
					.get(categoryIndex);
			for (int childIndex = 0; childIndex < listChildState.size(); childIndex++) {
				if (listChildState.get(childIndex).state) {
					listChildState.remove(childIndex);
					listCategory.get(categoryIndex).listChild
							.remove(childIndex);
					childIndex--;
				}
			}
			if (listChildState.size() == 0) {
				listChecked.remove(categoryIndex);
				listCategory.remove(categoryIndex);
				categoryIndex--;
			}
		}
		btnRemove.setText("Remove");
		btnRemove.setTextColor(resource.getColor(R.color.color_text_child));
		notifyDataSetChanged();
	}

	class CheckBoxState {
		boolean state;

		public CheckBoxState(boolean state) {
			this.state = state;
		}
	}
}
